package com.base.commonlib.widget;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.base.commonlib.R;
import com.base.commonlib.base.adapter.BaseAdapter;
import com.base.commonlib.base.adapter.BaseHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by arvin on 2018/10/25.
 */

public class SingleSearchDialog<T> extends BottomSheetDialog {
    private String mTitle;
    private List<T> mData=new ArrayList<>();
    private List<T> mData_initial=new ArrayList<>();
    private RecyclerView mRv;
    private Context mContext;
    private OnItemChooseListener mListener;
    private BaseAdapter<T> mAdapter;
    private EditText mEt_serach;

    public SingleSearchDialog(@NonNull Context context, String title, List<T> data, OnItemChooseListener listener) {
//        super(context, R.style.dialog);
        super(context);
        mTitle = title;
        mData.addAll(data);
        mData_initial.addAll(data);
        mListener=listener;
        mContext=context;
    }


    public SingleSearchDialog(@NonNull Context context, String title, OnItemChooseListener listener) {
//        super(context,R.style.dialog);
        super(context);
        mTitle = title;
        mContext = context;
        mListener = listener;
    }

//    public void setData(List<T> data)
//    {
//        mData.clear();
//        mData.addAll(data);
//        mAdapter.notifyDataSetChanged();
//    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        setContentView(R.layout.siglesearch_dialog_layout);
        TextView tv_title = findViewById(R.id.tv_title);
        mRv = findViewById(R.id.rv);
        mEt_serach = findViewById(R.id.et_search);
        tv_title.setText(mTitle);
        initRv();
        initSearch();
        super.onCreate(savedInstanceState);
    }

    private void initSearch() {
        mEt_serach.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String keyword = s.toString();
                mData.clear();
                if(TextUtils.isEmpty(keyword))
                {
                    mData.addAll(mData_initial);
                }else
                {
                    for(T item:mData_initial)
                    {
                        if(item.toString().contains(keyword))
                        {
                            mData.add(item);
                        }
                    }
                }
                mAdapter.notifyDataSetChanged();
                if (mData.size()>0) {
                    InputMethodManager inputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputMethodManager.hideSoftInputFromWindow(mEt_serach.getWindowToken(), 0);
                }
            }
        });
    }

    private void initRv() {
        mAdapter = new BaseAdapter<T>(mData, mContext) {

            @Override
            public void convert(BaseHolder holder, T o, boolean isHeader, boolean isBottom) {
                TextView tv = holder.getView(R.id.tv);
                tv.setText(o.toString());

            }
        };
        mAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseHolder holder, int dataPosition, boolean isHeader, boolean isBottom) {
                mListener.onItemChoose(mData.get(dataPosition));
                SingleSearchDialog.this.dismiss();
            }
        });
        mAdapter.putNormalType(R.layout.item_siglechoose_dialog);
        mRv.setLayoutManager(new LinearLayoutManager(mContext));
        mRv.setAdapter(mAdapter);
    }

    public interface OnItemChooseListener
    {
        void onItemChoose(Object o);
    }

    @Override
    public void show() {
//        setView(new EditText(mContext));
        super.show();

    }
}
